package com.heifeng.cloudshelf.mvp.programme.presenter;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;

import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseResponse;
import com.heifeng.cloudshelf.base.RxPresenter;
import com.heifeng.cloudshelf.constants.HTTPConstants;
import com.heifeng.cloudshelf.dao.Programme3DDetailBean;
import com.heifeng.cloudshelf.dao.ProgrammeBean;
import com.heifeng.cloudshelf.dao.ProgrammeImageBean;
import com.heifeng.cloudshelf.dao.Programmes3DBean;
import com.heifeng.cloudshelf.event.DialogSpecTextEvent;
import com.heifeng.cloudshelf.helper.MyObserver;
import com.heifeng.cloudshelf.helper.RxUtil;
import com.heifeng.cloudshelf.mvp.model.DetailImageInfo;
import com.heifeng.cloudshelf.mvp.model.ProgrammeSelectInfo;
import com.heifeng.cloudshelf.mvp.model.SpecMultipleEntity;
import com.heifeng.cloudshelf.mvp.programme.adapter.ProgrammeRvAdapter;
import com.heifeng.cloudshelf.mvp.programme.adapter.SpecMultipleItemRvAdapter;
import com.heifeng.cloudshelf.mvp.programme.contract.ProgrammeActivityContract;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.utils.LoginErrorUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2019/9/11.
 */

public class ProgrammeActivityPresenter extends RxPresenter<ProgrammeActivityContract.View> implements ProgrammeActivityContract.Presenter {

    private List<DetailImageInfo> list = new ArrayList<>();

    private LinearLayoutManager layoutManager;

    //private ProgrammeBottomRvAdapter adapter;

    private SpecMultipleItemRvAdapter adapter;

    private List<ProgrammeSelectInfo> infos = new ArrayList<>();
    private List<ProgrammeSelectInfo> backupInfos = new ArrayList<>();
    private List<DialogSpecTextEvent> events = new ArrayList<>();

    private Map<Integer, String> specTexts = new HashMap<>();

    private List<String> spaceIdsList = new ArrayList<>();
    private List<Integer> selectSpaceIdsIds = new ArrayList<>();

    private List<SpecMultipleEntity> mData = new ArrayList<>();
    private ProgrammeRvAdapter programmeRvAdapter;
    private int pageTotal = 1;
    private int pageIndex = 1;
    private int total = 0;
    public static int type = 0;

    public static List<ProgrammeBean.SpecListBean> allSpecList;


    @Inject
    public ProgrammeActivityPresenter() {
    }

    @Override
    public void init() {

    }

    private void initImageList(Object obj) {
        if (1 == pageIndex) {
            list.clear();
        }
        if (null != mView) {
            if (0 == mView.getType()) {
                //初始化方案库图片列表
                Programmes3DBean getresult = (Programmes3DBean) obj;
                if (null != getresult.getDataList() && getresult.getDataList().size() > 0) {
                    int count = 0;

                    if (list.size() > 0) {
                        DetailImageInfo detailImageInfo = list.get(list.size() - 1);
                        if (null == detailImageInfo.getFirstImage()) {
                            Programmes3DBean.DataListBean dataListBean = getresult.getDataList().get(0);
                            Log.e("3d", "datas：" + dataListBean.toString());
                            Programme3DDetailBean dDetailBean = new Programme3DDetailBean(dataListBean.getID() + "", dataListBean.getViewUrl(), dataListBean.getImageList());
                            List<String> imageList = dataListBean.getImageList();
                            String url = "";
                            if (null != imageList && imageList.size() > 0) {
                                url = imageList.get(0);
                            }
                            DetailImageInfo.ImageInfoBean urlImage = new DetailImageInfo.ImageInfoBean(url, dataListBean.getName(), mView.getType(), dDetailBean);
                            detailImageInfo.setFirstImage(urlImage);
                            count = 1;
                        }

                        if (null == detailImageInfo.getSecondImage() && getresult.getDataList().size() > 1) {
                            Programmes3DBean.DataListBean dataListBean = null;
                            if (null == detailImageInfo.getFirstImage()) {
                                dataListBean = getresult.getDataList().get(0);
                                count = 1;
                            } else {
                                dataListBean = getresult.getDataList().get(1);
                                count = 2;
                            }
                            List<String> imageList = dataListBean.getImageList();
                            String url = "";
                            if (null != imageList && imageList.size() > 0) {
                                url = imageList.get(0);
                            }
                            Programme3DDetailBean dDetailBean = new Programme3DDetailBean(dataListBean.getID() + "", dataListBean.getViewUrl(), dataListBean.getImageList());
                            DetailImageInfo.ImageInfoBean urlImage = new DetailImageInfo.ImageInfoBean(url, dataListBean.getName(), mView.getType(), dDetailBean);
                            detailImageInfo.setSecondImage(urlImage);

                        }
                    }

                    DetailImageInfo.ImageInfoBean urlImage;
                    DetailImageInfo.ImageInfoBean urlImage1;
                    while (getresult.getDataList().size() >= count) {
                        urlImage = null;
                        urlImage1 = null;
                        if (count < getresult.getDataList().size()) {
                            Programmes3DBean.DataListBean dataListBean = getresult.getDataList().get(count);
                            List<String> imageList = dataListBean.getImageList();
                            if (null != imageList && imageList.size() > 0) {
                                Programme3DDetailBean dDetailBean = new Programme3DDetailBean(getresult.getDataList().get(count).getID() + "", getresult.getDataList().get(count).getViewUrl(), imageList);
                                urlImage = new DetailImageInfo.ImageInfoBean(imageList.get(0), dataListBean.getName(), mView.getType(), dDetailBean);
                            }
                            if (count + 1 < getresult.getDataList().size()) {
                                dataListBean = getresult.getDataList().get(count + 1);
                                imageList = dataListBean.getImageList();
                                if (null != imageList && imageList.size() > 0) {
                                    Programme3DDetailBean dDetailBean = new Programme3DDetailBean(getresult.getDataList().get(count + 1).getID() + "", getresult.getDataList().get(count + 1).getViewUrl(), imageList);
                                    urlImage1 = new DetailImageInfo.ImageInfoBean(imageList.get(0), dataListBean.getName(), mView.getType(), dDetailBean);
                                }
                            }
                        } else if (getresult.getDataList().size() < count && count - 1 < getresult.getDataList().size()) {
                            Programmes3DBean.DataListBean dataListBean = getresult.getDataList().get(count);
                            List<String> imageList = dataListBean.getImageList();
                            if (null != imageList && imageList.size() > 0) {
                                Programme3DDetailBean dDetailBean = new Programme3DDetailBean(getresult.getDataList().get(count).getID() + "", getresult.getDataList().get(count).getViewUrl(), imageList);
                                urlImage = new DetailImageInfo.ImageInfoBean(imageList.get(0), dataListBean.getName(), mView.getType(), dDetailBean);
                            }
                        }
                        if (null != urlImage) {
                            DetailImageInfo info = new DetailImageInfo(urlImage, urlImage1);
                            list.add(info);
                        }
                        count += 2;
                    }
                }
            } else {
                //初始化产品大全图片列表
                List<ProgrammeImageBean.DataBean> data = (List<ProgrammeImageBean.DataBean>) obj;
                if (null != data && data.size() > 0) {
                    int count = 0;

                    if (list.size() > 0) {
                        DetailImageInfo detailImageInfo = list.get(list.size() - 1);
                        if (null == detailImageInfo.getFirstImage()) {
                            ProgrammeImageBean.DataBean dataBean = data.get(0);
                            DetailImageInfo.ImageInfoBean urlImage = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                            detailImageInfo.setFirstImage(urlImage);
                            count = 1;
                        }
                        if (null == detailImageInfo.getSecondImage() && data.size() > 1) {
                            ProgrammeImageBean.DataBean dataBean = null;
                            if (null == detailImageInfo.getFirstImage()) {
                                dataBean = data.get(0);
                                count = 1;
                            } else {
                                dataBean = data.get(1);
                                count = 2;
                            }
                            DetailImageInfo.ImageInfoBean urlImage = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                            detailImageInfo.setFirstImage(urlImage);
                        }
                    }

                    DetailImageInfo.ImageInfoBean urlImage;
                    DetailImageInfo.ImageInfoBean urlImage1;
                    while (data.size() >= count || data.size() == count - 1) {
                        urlImage = null;
                        urlImage1 = null;
                        if (count == data.size() - 1) {
                            ProgrammeImageBean.DataBean dataBean = data.get(count);
                            urlImage = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                        } else if (count < data.size()) {
                            ProgrammeImageBean.DataBean dataBean = data.get(count);
                            urlImage = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                            if (count + 1 < data.size()) {
                                dataBean = data.get(count + 1);
                                urlImage1 = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                            }
                        } else if (data.size() < count && count - 1 < data.size()) {
                            ProgrammeImageBean.DataBean dataBean = data.get(count - 1);
                            urlImage = new DetailImageInfo.ImageInfoBean(dataBean.getCloudImage(), dataBean.getTitle(), mView.getType(), dataBean.getId());
                        }
                        if (null != urlImage) {
                            DetailImageInfo info = new DetailImageInfo(urlImage, urlImage1);
                            list.add(info);
                        }
                        count += 2;
                    }
                }
            }

            Log.e("image", "list:" + list.size());
            mView.showError(false);

//            Glide.get(mContext).clearMemory();
            if (null == programmeRvAdapter) {
                LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
                mView.getRecyclerView1().setLayoutManager(layoutManager);
                programmeRvAdapter = new ProgrammeRvAdapter(mContext, R.layout.layout_programme_rv_item, list, mView.getType());
                programmeRvAdapter.setHasStableIds(true);
                ((DefaultItemAnimator) mView.getRecyclerView1().getItemAnimator()).setSupportsChangeAnimations(false); // 取消动画效果
                mView.getRecyclerView1().setAdapter(programmeRvAdapter);
            } else {
                programmeRvAdapter.setNewData(list);
            }
        }
    }

    @Override
    public void initRecyclerView(Object obj) {
        if (infos.size() > 0) {
            infos.clear();
        }
        int specTextId = 0;
        if (null != mView) {
            type = mView.getType();
            if (0 == mView.getType()) {
                Programmes3DBean getresult = (Programmes3DBean) obj;
                if (null == getresult) {
                    return;
                }
                if (null != getresult.getModelHouseType() && getresult.getModelHouseType().size() > 0) {
                    List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();
                    optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, 1, -1, "全部", true));
                    for (int i = 0; i < getresult.getModelHouseType().size(); i++) {
                        optionBeans.add(new ProgrammeSelectInfo.OptionBean(1, getresult.getModelHouseType().get(i).getID(), getresult.getModelHouseType().get(i).getName()));
                    }
                    ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
                    selectInfo.setTitle(mContext.getResources().getString(R.string.text_space));
                    selectInfo.setIcon(R.mipmap.ic_classification_space);
                    selectInfo.setOptions(optionBeans);
                    infos.add(selectInfo);
                }
                if (null != getresult.getProductStyle() && getresult.getProductStyle().size() > 0) {
                    List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();
                    optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, 2, -2, "全部", true));
                    for (int i = 0; i < getresult.getProductStyle().size(); i++) {
                        optionBeans.add(new ProgrammeSelectInfo.OptionBean(2, getresult.getProductStyle().get(i).getID(), getresult.getProductStyle().get(i).getName()));
                    }
                    ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
                    selectInfo.setTitle(mContext.getResources().getString(R.string.text_style));
                    selectInfo.setIcon(R.mipmap.ic_classification_style);
                    selectInfo.setOptions(optionBeans);
                    infos.add(selectInfo);
                }
            } else {
                List<ProgrammeBean.SpecListBean> specList = (List<ProgrammeBean.SpecListBean>) obj;
                if (null == specList || specList.size() == 0) {
                    return;
                }
                boolean isSeled = false;
                for (int i = 0; i < specList.size(); i++) {
                    String spec_name = specList.get(i).getSpec_name();
                    List<ProgrammeBean.SpecListBean.OptionsBean> options = specList.get(i).getOptions();
                    List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();
                    if (2 == mView.getType() && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                        isSeled = true;
                    } else {
                        optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, i + 1, 0 - i, "全部", true));
                    }
                    for (int j = 0; j < options.size(); j++) {
                        if (j == 0) {
                            if (isSeled && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                                specTextId = options.get(j).getId();
                            } else {
                                isSeled = false;
                            }
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(options.get(j).getParentId(), i + 1, options.get(j).getId(), options.get(j).getOptionName(), isSeled));
                        } else {
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(options.get(j).getParentId(), i + 1, options.get(j).getId(), options.get(j).getOptionName()));
                        }
                    }
                    ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
                    selectInfo.setTitle(spec_name);
                    selectInfo.setOptions(optionBeans);
                    if (spec_name.equals(mContext.getResources().getString(R.string.text_space))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_space);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_modelling)) || spec_name.equals(mContext.getResources().getString(R.string.text_category)) || spec_name.equals("品牌")) {
                        selectInfo.setIcon(R.mipmap.ic_classifiton_modelling);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_style)) || spec_name.equals("功能") || spec_name.equals(mContext.getResources().getString(R.string.text_serious))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_style);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                        selectInfo.setIcon(R.mipmap.ic_classifition_type);
                    }
                    infos.add(selectInfo);
                }
            }

            for (int i = 0; i < infos.size(); i++) {
                mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW, i));
            }

            layoutManager = new LinearLayoutManager(mContext);
            mView.getRecyclerView().setLayoutManager(layoutManager);

            adapter = new SpecMultipleItemRvAdapter(mContext, mData, infos);
            mView.getRecyclerView().setAdapter(adapter);

            if (null != mView && 2 == mView.getType()) {
                setSpecList(0, specTextId, 0);
            }
        }
    }

    @Override
    public void getSpecList(int templateId) {
        NetClient.getInstance().getNetApi().getSpecList(templateId)
                .compose(RxUtil.<BaseResponse<ProgrammeBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<ProgrammeBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<ProgrammeBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            allSpecList = baseResponse.getresult().getSpecList();
                            Log.e("TAGS", "size:" + allSpecList.size());
                            initRecyclerView(allSpecList);
                            if (2 == mView.getType()) {
                                if (null != allSpecList && allSpecList.size() > 0) {
                                    spaceIdsList.clear();
                                    int id = allSpecList.get(0).getOptions().get(0).getId();
                                    spaceIdsList.add(id + "");
                                }
                                toSearchBySpecOption(templateId);
                            }
                        } else {
                            LoginErrorUtils.loginOut(mContext, baseResponse.getCode(), baseResponse.getmsg());

                        }
                    }
                });
    }

    @Override
    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    @Override
    public void setSpecList(int position, int specTextId, int parentId) {
        Log.e("setSpecList", "Spec:" + position + ",specTextId:" + specTextId + ",parentId:" + parentId);

        if (0 == parentId && null != allSpecList && allSpecList.size() > 0) {
            boolean isSelAll = true;
            boolean isRelation = false;

            List<Integer> selIds = new ArrayList<>();

            //遍历已选择的选项
            for (int i = 0; i < infos.size(); i++) {
                List<ProgrammeSelectInfo.OptionBean> options = infos.get(i).getOptions();
                for (int j = 0; j < options.size(); j++) {
                    ProgrammeSelectInfo.OptionBean optionBean = options.get(j);
                    Log.e("setSpecList", "optionBean:" + optionBean);
                    if (optionBean.isSelected() && optionBean.getId() != specTextId) {
                        selIds.add(optionBean.getId());
                        if (0 != optionBean.getId()) {
                            isSelAll = false;
                        }
                    }
                }
            }

            //选择全部按钮，重置所有选项
            if (isSelAll && 0 == specTextId) {
                reSetData();
                return;
            }

            //判断点击全部按钮后，该列是否是属于需要联动的按钮
            if (0 == specTextId && infos.size() > position - 1) {
                boolean isOtherRelation = false;
                int id = infos.get(position - 1).getOptions().get(1).getId();
                //遍历按钮是否存在关联
                for (int i = 0; i < allSpecList.size(); i++) {
                    for (int j = 0; j < allSpecList.get(i).getOptions().size(); j++) {
                        if (id == allSpecList.get(i).getOptions().get(j).getParentId()) {
                            isOtherRelation = true;
                            break;
                        }
                    }
                    if (isOtherRelation) {
                        break;
                    }
                    Log.e("setSpecList", "for isOtherRelation:" + isOtherRelation);
                }

                if (isOtherRelation) {
                    reSetData();//重置所有数据
                    //回复已选的且并不关联的选项
                    for (int i = 0; i < infos.size(); i++) {
                        List<ProgrammeSelectInfo.OptionBean> options = infos.get(i).getOptions();
                        for (int j = 0; j < options.size(); j++) {
                            ProgrammeSelectInfo.OptionBean optionBean = options.get(j);
                            if (selIds.contains(optionBean.getId()) && 0 == optionBean.getParentId()) {
                                optionBean.setSelected(true);
                                if (0 < optionBean.getId()) {
                                    options.get(0).setSelected(false);
                                }
                                break;
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                    return;
                }
            }

            //遍历按钮是否存在关联
            for (int i = 0; i < allSpecList.size(); i++) {
                for (int j = 0; j < allSpecList.get(i).getOptions().size(); j++) {
                    if (0 != specTextId && specTextId == allSpecList.get(i).getOptions().get(j).getParentId()) {
                        isRelation = true;
                        break;
                    }
                }
                if (isRelation) {
                    break;
                }
            }
            Log.e("setSpecList", "for isRelation:" + isRelation);

            if (!isRelation) {
                return;
            }

            Log.e("setSpecList", "size:" + selIds.size() + ",selIds:" + selIds);

            List<ProgrammeSelectInfo> newInfos = new ArrayList<>();

            for (int i = 0; i < allSpecList.size(); i++) {
                boolean isSelected = false;

                ProgrammeBean.SpecListBean info = allSpecList.get(i);

                String spec_name = info.getSpec_name();
                List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();

                List<ProgrammeBean.SpecListBean.OptionsBean> options = info.getOptions();
                if (2 == mView.getType() && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                } else {
                    optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, i + 1, 0 - i, "全部"));
                }
                for (int j = 0; j < options.size(); j++) {
                    ProgrammeBean.SpecListBean.OptionsBean optionsBean = options.get(j);
                    if (!isSelected) {
                        isSelected = (optionsBean.getId() == specTextId);
                    }
                    if (0 == optionsBean.getParentId()) {
                        optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
                    } else {

                        if (optionsBean.getParentId() == specTextId) {
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
//                        }else{
//                            if (!isRelation) {
//                                optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
//                            }
                        }

                        if (selIds.contains(optionsBean.getParentId())) {
                            optionBeans.add(new ProgrammeSelectInfo.OptionBean(optionsBean.getParentId(), i + 1, optionsBean.getId(), optionsBean.getOptionName(), optionsBean.getId() == specTextId));
                        }

                    }
                }

                if (optionBeans.size() > 1) {
                    ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
                    selectInfo.setTitle(spec_name);

                    int index = 0;
                    if (isSelected) {
                        if ("全部".equals(optionBeans.get(0).getSpec_name())) {
                            optionBeans.get(0).setSelected(false);
                        }
                    } else {
                        for (int j = 0; j < optionBeans.size(); j++) {
                            ProgrammeSelectInfo.OptionBean optionBean = optionBeans.get(j);
                            if (selIds.contains(optionBean.getId())) {
                                index = j;
                            } else {
                                optionBean.setSelected(false);
                            }
                        }
                        optionBeans.get(index).setSelected(true);
                    }

                    selectInfo.setOptions(optionBeans);
                    if (spec_name.equals(mContext.getResources().getString(R.string.text_space))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_space);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_modelling)) || spec_name.equals(mContext.getResources().getString(R.string.text_category)) || spec_name.equals("品牌")) {
                        selectInfo.setIcon(R.mipmap.ic_classifiton_modelling);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_style)) || spec_name.equals("功能") || spec_name.equals(mContext.getResources().getString(R.string.text_serious))) {
                        selectInfo.setIcon(R.mipmap.ic_classification_style);
                    } else if (spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                        selectInfo.setIcon(R.mipmap.ic_classifition_type);
                    }
                    newInfos.add(selectInfo);
                }
            }

            if (newInfos.size() > 0) {
                infos.clear();
                mData.clear();
                infos.addAll(newInfos);
                for (int i = 0; i < infos.size(); i++) {
                    mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW, i));
                }
                adapter.setNewData(mData);
            }
        }
    }

    private void reSetData() {
        if (null == allSpecList || allSpecList.size() == 0) {
            return;
        }
        infos.clear();
        events.clear();
        mData.clear();
        for (int i = 0; i < allSpecList.size(); i++) {
            String spec_name = allSpecList.get(i).getSpec_name();
            List<ProgrammeBean.SpecListBean.OptionsBean> options = allSpecList.get(i).getOptions();
            List<ProgrammeSelectInfo.OptionBean> optionBeans = new ArrayList<>();
            if (2 == mView.getType() && spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
            } else {
                optionBeans.add(new ProgrammeSelectInfo.OptionBean(0, i + 1, 0 - i, "全部", true));
            }
            for (int j = 0; j < options.size(); j++) {
                optionBeans.add(new ProgrammeSelectInfo.OptionBean(options.get(j).getParentId(), i + 1, options.get(j).getId(), options.get(j).getOptionName()));
            }
            ProgrammeSelectInfo selectInfo = new ProgrammeSelectInfo();
            selectInfo.setTitle(spec_name);
            selectInfo.setOptions(optionBeans);
            if (spec_name.equals(mContext.getResources().getString(R.string.text_space))) {
                selectInfo.setIcon(R.mipmap.ic_classification_space);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_modelling)) || spec_name.equals(mContext.getResources().getString(R.string.text_category)) || spec_name.equals("品牌")) {
                selectInfo.setIcon(R.mipmap.ic_classifiton_modelling);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_style)) || spec_name.equals("功能") || spec_name.equals(mContext.getResources().getString(R.string.text_serious))) {
                selectInfo.setIcon(R.mipmap.ic_classification_style);
            } else if (spec_name.equals(mContext.getResources().getString(R.string.text_type))) {
                selectInfo.setIcon(R.mipmap.ic_classifition_type);
            }
            infos.add(selectInfo);
        }
        for (int i = 0; i < infos.size(); i++) {
            mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW, i));
        }
        adapter.setNewData(mData);
    }

    @Override
    public void setOpenSpSpecList(boolean isDismiss, boolean isChanged) {
        if (isDismiss) {
//            Log.e("SpecEvent", "isDismiss selectSpaceIdsIds:" + selectSpaceIdsIds);
//            Log.e("SpecEvent", "isDismiss isChanged:" + isChanged);
//            Log.e("SpecEvent", "infos:" + backupInfos);
            if (isChanged) {
                infos.clear();
                infos.addAll(backupInfos);
            }
            for (int i = 0; i < infos.size(); i++) {
                List<ProgrammeSelectInfo.OptionBean> options = infos.get(i).getOptions();
                for (int k = 0; k < options.size(); k++) {
                    ProgrammeSelectInfo.OptionBean optionBean = options.get(k);
                    if (selectSpaceIdsIds.contains(optionBean.getId())) {
                        optionBean.setSelected(true);
                    } else {
                        optionBean.setSelected(false);
                    }
                }
            }
        }
//        Log.e("SpecEvent", "infos:" + infos);
        selectSpaceIdsIds.clear();
        backupInfos.clear();
        mData.clear();
        for (int i = 0; i < infos.size(); i++) {
            mData.add(new SpecMultipleEntity(SpecMultipleEntity.TYPE_VIEW, i));
        }
        adapter.setNewData(mData);

    }

    @Override
    public void keepSelectSpSpec() {
        selectSpaceIdsIds.clear();
        backupInfos.clear();
        backupInfos.addAll(infos);
        for (int i = 0; i < infos.size(); i++) {
            for (int j = 0; j < infos.get(i).getOptions().size(); j++) {
                ProgrammeSelectInfo.OptionBean optionBean = infos.get(i).getOptions().get(j);
                if (optionBean.isSelected()) {
                    selectSpaceIdsIds.add(optionBean.getId());
                }
            }
        }
        Log.e("SpecEvent", "IDS:" + selectSpaceIdsIds);
    }

    @Override
    public void toSearchBySpecOption(int templateId) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageIndex);
        map.put("pageSize", 10);
        map.put("templateId", templateId);
        getOptions();
        if (null != spaceIdsList && spaceIdsList.size() > 0) {
            String ids = toGetSpaceIds();
            map.put("specificationOptionIds", ids);
//            map.put("specificationOptionIds", spaceIdsList);
            Log.e("TAGS", "group:" + spaceIdsList);
        }
        NetClient.getInstance().getNetApi().toSearchBySpecOption(map)
                .compose(RxUtil.<BaseResponse<ProgrammeImageBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<ProgrammeImageBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<ProgrammeImageBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            initImageList(baseResponse.getresult().getData());
//                            if (null != list && list.size() > 0) {
//                                total += baseResponse.getresult().getPageTotal();
//                            }else{
                            int count = baseResponse.getresult().getData().size();
                            if (1 == pageIndex) {
                                total = count;
                            } else {
                                total += count;
                            }
//                            }
                            if (null != mView) {
                                mView.setListSize(baseResponse.getresult().getPageTotal());
                            }
//                            pageTotal = baseResponse.getresult().getPageTotal();
                            int total = baseResponse.getresult().getPageTotal() / 10;
                            if (baseResponse.getresult().getPageTotal() % 10 != 0) {
                                total += 1;
                            }
                            pageTotal = total;
                        } else {
                            if (null != mView) {
                                mView.setListSize(0);
                            }
                            noSearchData(true);
                            LoginErrorUtils.loginOut(mContext, baseResponse.getCode(), baseResponse.getmsg());
                        }
                    }
                });
    }

    private String toGetSpaceIds() {
        StringBuffer ids = new StringBuffer();
        for (int i = 0; i < spaceIdsList.size(); i++) {
            if (i == spaceIdsList.size() - 1) {
                ids.append(spaceIdsList.get(i) + "");
            } else {
                ids.append(spaceIdsList.get(i) + ",");
            }
            // ids[i] = spaceIdsList.get(i);
        }
        return ids.toString();
    }

    @Override
    public void getTableModelHouse() {
//        RoomId  "pageNo"   pageSize  openid  PStyleId
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageIndex);
        map.put("pageSize", 10);
        map.put("openid", HTTPConstants.userId);

        getOptions();
        if (null != specTexts && specTexts.size() > 0) {
            if (specTexts.containsKey(1)) {
                String o = specTexts.get(1);
                map.put("RoomId", o);
            }
            if (specTexts.containsKey(2)) {
                String o = specTexts.get(2);
                map.put("PStyleId", o);
            }
        }

        NetClient.getInstance().getNetApi().getTableModelHouse(HTTPConstants.TABLE_MODEL_HOUSE, map)
                .compose(RxUtil.<BaseResponse<Programmes3DBean>>rxSchedulers())
                .subscribe(new MyObserver<BaseResponse<Programmes3DBean>>(mContext) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<Programmes3DBean> baseResponse) {
                        if (0 == baseResponse.getCode()) {
                            if (infos.size() == 0) {
                                initRecyclerView(baseResponse.getresult());
                            }

                            int count = null == baseResponse.getresult().getDataList() ? 0 : baseResponse.getresult().getDataList().size();
                            if (1 == pageIndex) {
                                total = count;
                            } else {
                                total += count;
                            }

                            initImageList(baseResponse.getresult());

                            if (null != mView) {
                                mView.setListSize(baseResponse.getresult().getTotal());
                            }
                            noSearchData(0 == count);
                            int total = baseResponse.getresult().getTotal() / 10;
                            if (baseResponse.getresult().getTotal() % 10 != 0) {
                                total += 1;
                            }
                            pageTotal = total;
                        } else {
                            if (null != mView) {
                                mView.setListSize(0);
                            }
                            LoginErrorUtils.loginOut(mContext, baseResponse.getCode(), baseResponse.getmsg());
                        }
                    }
                });
    }

    private void getOptions() {
        spaceIdsList.clear();
        specTexts.clear();
        for (int i = 0; i < infos.size(); i++) {
            for (int j = 0; j < infos.get(i).getOptions().size(); j++) {
                ProgrammeSelectInfo.OptionBean optionBean = infos.get(i).getOptions().get(j);
                if (optionBean.isSelected()) {
                    if (0 < optionBean.getId()) {
                        spaceIdsList.add(optionBean.getId() + "");
                        specTexts.put(i + 1, optionBean.getId() + "");
                    }
                }
            }
        }
    }

    private void noSearchData(boolean isNoData) {
        if (isNoData) {
            list.clear();
            if (null != programmeRvAdapter) {
                programmeRvAdapter.setNewData(list);
            }
            if (null != mView) {
                mView.showError(isNoData);
            }
        }
    }

    @Override
    public void putSpecTextsInGroup(int groupId, int specTextsId, DialogSpecTextEvent event) {
//        if (0 == specTextsId) {
//        spaceIdsList.clear();
//        specTexts.clear();
//        } else {
//            if (specTexts.containsKey(groupId)) {
//                String integer = specTexts.get(groupId);
//                if (spaceIdsList.contains(integer)) {
//                    spaceIdsList.remove(integer);
//                }
//                specTexts.remove(groupId);
//            }
//            specTexts.put(groupId, specTextsId + "");
//            spaceIdsList.add(specTextsId + "");
//            Log.e("TAGS","group map:"+specTexts);
//        }
        if (null != event) {
            events.add(event);
        }


        Log.e(" putSpecTexts", "map:" + specTexts);
    }

    @Override
    public boolean isVisBottom(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //屏幕中最后一个可见子项的position
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        //当前屏幕所看到的子项个数
        int visibleItemCount = layoutManager.getChildCount();
        //当前RecyclerView的所有子项个数
        int totalItemCount = layoutManager.getItemCount();
        //RecyclerView的滑动状态
        int state = recyclerView.getScrollState();

        if (visibleItemCount > 0 && lastVisibleItemPosition == totalItemCount - 1 && state == recyclerView.SCROLL_STATE_IDLE) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void nextPage(int templateId) {
        Log.e("onScrolled", "pageIndex:" + pageIndex + ",pageTotal:" + pageTotal);
        if (pageIndex + 1 <= pageTotal) {
            pageIndex += 1;
            if (null != mView && 0 != mView.getType()) {
                toSearchBySpecOption(templateId);
            } else {
                getTableModelHouse();
            }
        }
    }

    /**
     * @param list
     * @param len
     * @return 按指定大小，分隔集合，将集合按规定个数分为n个部分
     */
    public static List<List<DetailImageInfo>> splitList(List<DetailImageInfo> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return new ArrayList<>();
        }
        List<List<DetailImageInfo>> result = new ArrayList<List<DetailImageInfo>>();
        int size = list.size();
        int count = (size + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<DetailImageInfo> subList = new ArrayList<>();
            subList.addAll(list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1))));
            result.add(subList);
        }
        return result;
    }

}
